"""
Case Type   : HugePage
Case Name   : enable_huge_pages参数主备设置不同
Create At   : 2024/11/08
Owner       : liu-tong-8848
Description :
              1、主机设置参数为on
              2、备机设置参数为off
              3、重启数据库两次
              4、分别连接主备查询enable_huge_pages
              5、清理环境
Expect      :
              1、成功
              2、成功
              3、成功
              4、成功
              5、清理环境成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GucTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.com = Common('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.standby_sh = CommonSH('Standby1DbUser')
        self.db_user_node = Node(node='PrimaryDbUser')
        self.db_root_node = Node(node='PrimaryRoot')
        self.db_st_root_node = Node(node='Standby1Root')

    def test_startdb(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')

        step1 = '--step1:主机设置参数为on;expect:成功'
        self.log.info(step1)
        gs_guc_set_cmd = "enable_huge_pages=on"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step1)

        step2 = '--step2:备机设置参数为off;expect:成功'
        self.log.info(step2)
        gs_guc_set_cmd = "enable_huge_pages=off"
        gs_guc_set_result = self.standby_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step2)

        step3 = '--step3:重启数据库两次;expect:成功'
        self.log.info(step3)
        self.log.info("修改参数保证数据库可以启动，shared_buffers = 128MB，系统大页数1000")
        gs_guc_set_cmd = "shared_buffers = 128MB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.log.info(gs_guc_set_result)
        self.assertTrue(gs_guc_set_result, "执行失败" + step3)
        self.primary_sh.restart_db_cluster()
        change_hugepages_cmd = "echo 1000 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.log.info(change_hugepages_result)
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step3)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step3)
        time.sleep(2)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step3)

        step4 = '--step4:分别连接主备查询enable_huge_pages;expect:主机为on,备机为off,成功'
        self.log.info(step4)
        gs_guc_check_cmd = "source " + macro.DB_ENV_PATH + ";gs_guc check -D " + macro.DB_INSTANCE_PATH + \
                           " -N all -c 'enable_huge_pages'"
        result = self.db_user_node.sh(gs_guc_check_cmd).result()
        self.log.info(result)
        primary_hostname = self.db_root_node.sh("hostname").result()
        standby_hostname = self.db_st_root_node.sh("hostname").result()
        primary_parameter = '[' + primary_hostname + ']  enable_huge_pages=on'
        standby_parameter = '[' + standby_hostname + ']  enable_huge_pages=off'
        self.assertIn(primary_parameter, result, "执行失败" + step4)
        self.assertIn(standby_parameter, result, "执行失败" + step4)

    def tearDown(self):
        step5 = '--step5:清理环境;expect:清理环境成功'
        self.log.info(step5)
        gs_guc_set_cmd = "enable_huge_pages = off"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.log.info(gs_guc_set_result)
        self.assertTrue(gs_guc_set_result, "执行失败" + step5)
        self.primary_sh.restart_db_cluster()
        result = self.com.show_param("enable_huge_pages")
        self.assertIn('off', result, "执行失败" + step5)
        use_sysctl_cmd = "sysctl -p"
        self.db_root_node.sh(use_sysctl_cmd)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
